<template>
  <div class="app-container">
    <!-- 顶部功能 -->
    <div class="filter-container" style="margin-bottom: 15px">
      <!-- 一些按钮 -->
      <el-button icon="el-icon-back" @click="$emit('close-event')">返 回</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-edit"
        @click="handleAdd"
      >添 加</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px"
        type="success"
        icon="el-icon-check"
        @click="handleSave"
      >保 存</el-button>
    </div>

    <!-- 表格 -->
    <el-table ref="formRef" :data="tableData" border>
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column prop="name" label="章节名" align="center">
        <template slot-scope="scope">
          <el-form :model="scope.row"  ref="formRef">
            <el-form-item prop="name" label="章节名" >
              <el-input v-model="scope.row.name" />
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="questionCount" label="题目数量" width="100" align="center" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="success" size="small" icon="el-icon-top" circle @click="handleUp(scope.$index, scope.row)" />
          <el-button type="primary" size="small" icon="el-icon-bottom" circle @click="handleDown(scope.$index, scope.row)" />
          <el-button type="danger" size="small" icon="el-icon-delete" circle @click="handleRemove(scope.$index, scope.row)" />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import Vue from 'vue'
import {listChapter, addChapter, updateChapter, saveChapters} from '@/api/teacher/chapter'

export default {
  name: 'Chapter',
  props: {
    bId: {
      type: Number,
      required: true,
      default: 1
    }
  },
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 章节表格数据
      tableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        bId: null,
        name: null,
        radioNum: null,
        checkNum: null,
        judgmentNum: null,
        blankNum: null,
        orderNum: null,
        shortAnswerNum: null,
        remark: null
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询章节列表 */
    getList() {
      listChapter({bId: this.bId}).then(response => {
        if (response.code === 200) {
          this.tableData = response.data
        }
      });
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        name: null,
        tId: null,
        updataTime: null
      };
      this.getList();
    },


    // 点击添加
    handleAdd() {
      const row = {
        id: null,
        bId: this.bId,
        name: null,
        questionCount: 0
      };
      this.tableData.push(row);
    },
// 表单重置
    reset() {
      this.form = {
        id: null,
        bId: null,
        name: null,
        radioNum: null,
        checkNum: null,
        judgmentNum: null,
        blankNum: null,
        orderNum: null,
        shortAnswerNum: null,
        remark: null
      };
      this.resetQuery("form");
    },
    // 点击移除
    handleRemove(index, row) {
      if (row.questionCount !== 0) {
        this.$confirm('参照该章节的题目的章节属性将被置空，确认要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.tableData.splice(index, 1);
        });
      } else {
        this.tableData.splice(index, 1);
      }
    },

    // 上移记录
    handleUp(index, row) {
      if (index === 0) {
        this.$message.warning('已经是第一行了，不能上移');
        return;
      }
      const temp = this.tableData[index - 1];
      Vue.set(this.tableData, index - 1, this.tableData[index]);
      Vue.set(this.tableData, index, temp);
      this.$message.success('上移成功');
    },

    // 下移记录
    handleDown(index, row) {
      if (index === (this.tableData.length - 1)) {
        this.$message.warning('已经是最后一行了，不能下移');
        return;
      }
      const temp = this.tableData[index + 1];
      Vue.set(this.tableData, index + 1, this.tableData[index]);
      Vue.set(this.tableData, index, temp);
      this.$message.success('下移成功');
    },

    // 点击保存
    handleSave() {
      // 把tableData传给服务器
      // console.log("****************")
      // console.log(this.tableData[0])
      saveChapters(this.tableData).then(res => {
        if (res.code === 200) {
          this.$message.success('保存成功')
          this.$emit('close-event')
        } else {
          this.$message.error('保存失败')
        }
      })
    }

  },

};
</script>

<style scoped>
</style>
